﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.DAL;
using Core.Extentions;
using Core.Utils;

namespace Core.BOL
{
    public class ConvocRepository
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public ConvocRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void InsertConvoc(tSite_Convoc convoc)
        {
            context.tSite_Convocs.InsertOnSubmit(convoc);
        }

        public List<tSite_Convoc> GetAllConvoc()
        {
            return GetAllConvoc(0,-1);
        }

        public List<tSite_Convoc> GetAllConvoc(int skip, int take)
        {
            var query = (from c in context.tSite_Convocs
                         join s in context.tSite_Sections on c.SID equals s.ID
                         orderby c.InsertDate descending
                         select c);

            if (take > -1)
            {
                return query.Skip(skip).Take(take).ToList();
            }

            return query.ToList();
        }

        public List<tSite_Convoc> GetAllCurrentConvoc()
        {
            List<tSite_Convoc> query = (from c in context.tSite_Convocs
                                        where c.StartDate >= DateTime.Now.GetNowTimeByTimeZone() || c.EndDate >= DateTime.Now.GetNowTimeByTimeZone()
                                        join s in context.tSite_Sections on c.SID equals s.ID
                                        orderby c.InsertDate descending
                                        select c).ToList();
            return query;
        }

        public List<tSite_Convoc> GetConvocBySystemName(string systemName, int skip, int take)
        {
            return GetConvocBySystemName(systemName, skip, take, -1);
        }

        public List<tSite_Convoc> GetConvocBySystemName(string systemName, int skip, int take, int except)
        {
            var query = (from c in context.tSite_Convocs
                         join s in context.tSite_Sections on c.SID equals s.ID
                         where s.SystemName == systemName
                         orderby c.StartDate descending
                         select c);

            if (except != -1)
            {
                query = query.Where(p => p.ID != except); 
            }

            if (take != 0)
            {
                query = query.Skip(skip).Take(take);
            }

            
            return query.ToList();
        }

        public tSite_Convoc GetConvocByID(int ID)
        {
            var query = (from c in context.tSite_Convocs
                         where c.ID == ID
                         select c).Take(1).SingleOrDefault();
            return query;
        }

        public tSite_Convoc GetNextConvocBySystemName(string systemName)
        {
            var query = (from c in context.tSite_Convocs
                         join s in context.tSite_Sections on c.SID equals s.ID
                         where c.StartDate >= DateTime.Now.GetNowTimeByTimeZone() && c.EndDate >= DateTime.Now.GetNowTimeByTimeZone() && s.SystemName == systemName.ToLower()
                         orderby c.StartDate descending
                         select c).Take(1).SingleOrDefault();
            return query; 
        }

        public tSite_Convoc GetCurrentConvocBySystemName(string systemName)
        {
            var query = (from c in context.tSite_Convocs
                         join s in context.tSite_Sections on c.SID equals s.ID
                         where c.StartDate <= DateTime.Now.GetNowTimeByTimeZone() && c.EndDate >= DateTime.Now.GetNowTimeByTimeZone() && s.SystemName == systemName.ToLower()
                         orderby c.StartDate descending
                         select c).Take(1).SingleOrDefault();
            return query;
        }

        public void Delete(int CID)
        {
            tSite_Convoc tConvoc = context.tSite_Convocs.SingleOrDefault(p => p.ID == CID);
            if (tConvoc != null)
            {
                tSite_New tNews = context.tSite_News.SingleOrDefault(p => p.ID == tConvoc.NID);
                if (tNews != null)
                {
                    context.tSite_News.DeleteOnSubmit(tNews);
                }
                context.tSite_Convocs.DeleteOnSubmit(tConvoc);
            }
            
        }

        public void Save()
        {
            context.SubmitChanges();
        }
        #endregion
    }
}
